{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "运用 决策树 预测分类结果： [0 0 2]\n"
     ]
    }
   ],
   "source": [
    "#鸢尾花\n",
    "\n",
    "from sklearn.datasets import load_iris #导入鸢尾花训练集\n",
    "from sklearn.tree import DecisionTreeClassifier #导入决策树分类器\n",
    "from sklearn.model_selection import cross_val_score #导入计算交叉检验值的函数cross_val_score\n",
    "\n",
    "#创建一颗基于基尼系数的决策树\n",
    "clf = DecisionTreeClassifier()\n",
    "#将鸢尾花数据赋值给变量iris\n",
    "iris = load_iris()\n",
    "\n",
    "data = iris.data #鸢尾花数据作为特征，\n",
    "target = iris.target #鸢尾花数据作为目标结果[0,1,2]，\n",
    "cv = 10 #使用10折交叉验证\n",
    "cross_val_score(clf, data, target, cv = 10)\n",
    "\n",
    "\n",
    "clf.fit(data, target) #fit()训练模型\n",
    "test = [[5.1, 3.5, 1.4, 0.2],\n",
    " [4., 2.2, 1.7, 0.4],\n",
    " [5.9, 3., 5.1, 1.8]]\n",
    "pre_test_tree = clf.predict(test) #决策树 函数预测\n",
    "print('运用 决策树 预测分类结果：',pre_test_tree)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "运用 决策树 预测分类结果： [0 2 1]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\ProgramData\\Anaconda3\\lib\\site-packages\\sklearn\\model_selection\\_split.py:581: Warning: The least populated class in y has only 4 members, which is too few. The minimum number of groups for any class cannot be less than n_splits=10.\n",
      "  % (min_groups, self.n_splits)), Warning)\n"
     ]
    }
   ],
   "source": [
    "#隐形眼镜\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.preprocessing import LabelEncoder  # 用于序列化\n",
    "from sklearn.tree import DecisionTreeClassifier  # 导入决策树分类器\n",
    "from sklearn.model_selection import cross_val_score  # 导入计算交叉检验值的函数cross_val_score\n",
    "\n",
    "fr = open('lenses.txt', 'r')\n",
    "lenses = [inst.strip().split('\\t') for inst in fr.readlines()]  # 去掉空格，提取每组数据的类别，保存在列表里\n",
    "lensesLabels = ['age', 'prescript', 'astigmatic', 'tearRate']  # 特征标签\n",
    "lenses_target = [row[-1] for row in lenses]  # 目标标签\n",
    "# print(lenses_target)\n",
    "\n",
    "lenses_list = []  # 保存lenses数据的临时列表\n",
    "lenses_dict = {}  # 保存lenses数据的字典，用于生成pandas\n",
    "for each_label in lensesLabels:\n",
    "    # 提取信息，生成字典\n",
    "    for each in lenses:\n",
    "        lenses_list.append(each[lensesLabels.index(each_label)])\n",
    "    lenses_dict[each_label] = lenses_list\n",
    "    lenses_list = []\n",
    "\n",
    "lenses_data = pd.DataFrame(lenses_dict)  # 生成pandas.DataFrame\n",
    "# print(lenses_data)\n",
    "le = LabelEncoder()  # 创建LabelEncoder()对象，用于序列化\n",
    "for col in lenses_data.columns:\n",
    "    # 为每一列序列化\n",
    "    lenses_data[col] = le.fit_transform(lenses_data[col])\n",
    "\n",
    "lenses_target = le.fit_transform(lenses_target) # 将标签序列化\n",
    "# print(lenses_target)\n",
    "# print(lenses_data)\n",
    "\n",
    "lenses_data = np.array(lenses_data)\n",
    "lenses_target = np.array(lenses_target)\n",
    "\n",
    "# 创建一颗基于基尼系数的决策树\n",
    "clf = DecisionTreeClassifier(criterion=\"gini\", max_features=None)\n",
    "score = cross_val_score(clf, lenses_data, lenses_target, cv = 10) # 使用10折交叉验证\n",
    "\n",
    "clf.fit(lenses_data, lenses_target) # fit()训练模型\n",
    "# 待预测的测试集\n",
    "test = [\n",
    "    [1, 1, 1, 0],\n",
    "    [2, 0, 1, 0],\n",
    "    [0, 1, 0, 1]]\n",
    "pre_test_tree = clf.predict(test) # 决策树，函数预测\n",
    "print('运用 决策树 预测分类结果：',pre_test_tree)\n",
    "\n",
    "\n",
    "\n",
    "#报错原因：10折交叉验证取的训练模型和测试集数目不对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
